package cn.hackcloud.mutithread.mutilthread;

import java.util.concurrent.TimeUnit;

/**
 * 
 * @author hackcloud
 *
 */
public class TestVolatile {
	public static void main(String[] args) {
		MyVolatile myVolatile = new MyVolatile();

		new Thread(() -> {
			try {
				TimeUnit.SECONDS.sleep(3);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			myVolatile.add();
			System.out.println(Thread.currentThread().getName() + ":" + myVolatile.c);
		}, "A").start();
		while (myVolatile.c == 0) {

		}
		System.out.println("C不为0");

	}

}

class MyVolatile {
	volatile int c = 0;

	public void add() {
		this.c = 10;
	}
}